package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.dto.OrdersDto;
import com.itheima.entity.AddressBook;
import com.itheima.entity.Orders;
import com.itheima.entity.User;
import com.itheima.service.AddressBookService;
import com.itheima.service.OrdersService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;


    /**
     * 用户下单
     * @param order
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order){
        log.info("订单数据: {}",order);

        ordersService.submit(order);

        return R.success("下单成功");
    }


    @GetMapping("/page")
    public R<Page<OrdersDto>> page(int page, int pageSize, String number, String beginTime,  String endTime){

        log.info("分页条件查询时间范围参数 ===> {},{}",beginTime,endTime);

        Page<Orders> pageInfo = new Page<>(page,pageSize);
        Page<OrdersDto> pageDto = new Page<>();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number != null,Orders::getNumber,number);

        queryWrapper.gt(beginTime != null,Orders::getOrderTime,beginTime)
                        .lt(endTime!= null,Orders::getOrderTime,endTime);

        queryWrapper.orderByAsc(Orders::getOrderTime);
        ordersService.page(pageInfo, queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(pageInfo,pageDto,"records");
        List<Orders> records = pageInfo.getRecords();

        List<OrdersDto> list = records.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            //对象拷贝
            BeanUtils.copyProperties(item,ordersDto);
            //分类id
            Long userId = item.getUserId();
            //根据分类id查询分类对象
            LambdaQueryWrapper<AddressBook> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(AddressBook::getUserId,userId);
            AddressBook addressBook = addressBookService.getOne(lambdaQueryWrapper);
            User user = userService.getById(userId);

            if (user != null){
                // 分类名称
                String name = user.getName();
                ordersDto.setUserName(name);
                String consignee = addressBook.getConsignee();
                ordersDto.setConsignee(consignee);
            }
            return ordersDto;
        }).collect(Collectors.toList());

        pageDto.setRecords(list);
        return R.success(pageDto);
    }
}
